package com.chunayev.numerology.gui.frame;

import static com.chunayev.numerology.util.Constants.RESOURCE_BUNDLE;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;

import com.chunayev.numerology.gui.action.AbstractTokenAction;
import com.chunayev.numerology.gui.action.CalculateAction;
import com.chunayev.numerology.gui.action.SaveMiddlenameTokenAction;
import com.chunayev.numerology.gui.action.SaveNameTokenAction;
import com.chunayev.numerology.gui.action.SaveSurnameTokenAction;
import com.chunayev.numerology.gui.action.SaveTitleTokenAction;
import com.chunayev.numerology.gui.table.NameCalculationsTable;
import com.chunayev.numerology.gui.table.NumerologicTable;
import com.chunayev.numerology.gui.table.TokensTablePopupMenu;
import com.chunayev.numerology.gui.table.listener.NumerologicTableMouseDoubleClickListener;
import com.chunayev.numerology.gui.table.listener.StatisticsTableSelectionListener;
import com.chunayev.numerology.gui.table.listener.TokensMenuInvoker;
import com.chunayev.numerology.gui.table.listener.TokensTableSelectionListener;
import com.chunayev.numerology.util.Constants;

/**
 * @author Sergey Chunayev
 */
public class NumerologyFrame extends JFrame {
    
    private static final long serialVersionUID = 6882809531082139920L;
    
    private final JButton calculateButton;
    
    private final NumerologicTable numerologicTable;
    
    private final NameCalculationsTable statisticsTable;
    private final JTextArea textArea;
    private final NameCalculationsTable tokensStatisticsTable;
    
    private static final AbstractTokenAction saveNameTokenAction = new SaveNameTokenAction();
    private static final AbstractTokenAction saveSurnameTokenAction = new SaveSurnameTokenAction();
    private static final AbstractTokenAction saveMiddlenameTokenAction = new SaveMiddlenameTokenAction();
    private static final AbstractTokenAction saveTitleTokenAction = new SaveTitleTokenAction();
    private static final AbstractTokenAction[] saveTokenActions = {
            saveNameTokenAction, saveSurnameTokenAction,
            saveMiddlenameTokenAction, saveTitleTokenAction };
    
    /**
     * 
     */
    public NumerologyFrame() {
        super(RESOURCE_BUNDLE.getString("frame.title"));
        final Container c = getContentPane();
        
        final JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout(5, 5));
        final JPanel inputPanel = new JPanel(new BorderLayout());
        mainPanel.add(inputPanel, BorderLayout.NORTH);
        final JPanel textAreasPanel = new JPanel(new BorderLayout());
        final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        final JPanel textAreaPanel = new JPanel(new BorderLayout(0, 0));
        textAreaPanel.setBackground(Color.white);
        textArea = new JTextArea(5, 30);
        textAreaPanel.add(new JScrollPane(textArea));
        textAreaPanel.setBorder(new TitledBorder(RESOURCE_BUNDLE
                .getString("frame.textAreaPanel.title")));
        splitPane.add(textAreaPanel);
        final JPanel statisticsPanel = new JPanel(new BorderLayout(0, 0));
        statisticsPanel.setBorder(new TitledBorder(RESOURCE_BUNDLE
                .getString("frame.statisticsPanel.title")));
        
        statisticsTable = new NameCalculationsTable();
        statisticsTable.setSelectionBackground(Constants.mainPositionColor);
        statisticsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        statisticsPanel.add(new JScrollPane(statisticsTable));
        
        statisticsPanel.setBackground(Color.white);
        splitPane.add(statisticsPanel);
        textAreasPanel.add(splitPane);
        inputPanel.add(textAreasPanel, BorderLayout.CENTER);
        
        calculateButton = new JButton(new CalculateAction(this));
        inputPanel.add(calculateButton, BorderLayout.SOUTH);
        
        numerologicTable = new NumerologicTable();
        final Dimension d = numerologicTable
                .getPreferredScrollableViewportSize();
        d.height = 280;
        numerologicTable.setPreferredScrollableViewportSize(d);
        addComponentListener(new ComponentAdapter() {
            
            /*
             * (non-Javadoc)
             * @see
             * java.awt.event.ComponentAdapter#componentResized(java.awt.event
             * .ComponentEvent)
             */
            @Override
            public void componentResized(final ComponentEvent e) {
                super.componentResized(e);
                final int newHeight = numerologicTable.getParent().getHeight() / 7;
                if (newHeight > 35) {
                    numerologicTable.setRowHeight(newHeight);
                }
            }
        });
        mainPanel.add(new JScrollPane(numerologicTable), BorderLayout.CENTER);
        
        c.add(mainPanel);
        
        tokensStatisticsTable = getNameCalculationsTable();
        
        final StatisticsTableSelectionListener listener = new StatisticsTableSelectionListener(
                numerologicTable, statisticsTable, tokensStatisticsTable);
        
        statisticsPanel.add(new JScrollPane(tokensStatisticsTable),
                BorderLayout.SOUTH);
        
        statisticsTable.getSelectionModel().addListSelectionListener(listener);
        
        final TokensTableSelectionListener tokensTableListener = new TokensTableSelectionListener();
        tokensTableListener.setNumerologicTable(numerologicTable);
        tokensTableListener.setTokensStatisticsTable(tokensStatisticsTable);
        
        tokensStatisticsTable.getSelectionModel().addListSelectionListener(
                tokensTableListener);
        
        numerologicTable
                .addMouseListener(new NumerologicTableMouseDoubleClickListener());
    }
    
    public String getEnteredData() {
        return textArea.getText();
    }
    
    /**
     * @return the statisticsTable
     */
    public NameCalculationsTable getStatisticsTable() {
        return this.statisticsTable;
    }
    
    public void setData(final List<String> data) {
        this.statisticsTable.getModel().setData(data);
    }
    
    private NameCalculationsTable getNameCalculationsTable() {
        NameCalculationsTable nameCalculationsTable = new NameCalculationsTable();
        nameCalculationsTable
                .setSelectionBackground(Constants.otherPositionColor);
        final Dimension tokensTableDimension = nameCalculationsTable
                .getPreferredScrollableViewportSize();
        tokensTableDimension.height = 50;
        nameCalculationsTable
                .setPreferredScrollableViewportSize(tokensTableDimension);
        nameCalculationsTable.getTableHeader().setReorderingAllowed(false);
        nameCalculationsTable.getSelectionModel().setSelectionMode(
                ListSelectionModel.SINGLE_SELECTION);
        nameCalculationsTable.addMouseListener(new TokensMenuInvoker(
                new TokensTablePopupMenu(saveTokenActions)));
        
        for (AbstractTokenAction action : saveTokenActions) {
            action.setSummaryTokenChangeNotifier(nameCalculationsTable);
        }
        
        return nameCalculationsTable;
    }
    
}
